package ie.dkit.semanticPhotoWeb.client.panels.album;

import java.util.List;

import ie.dkit.semanticPhotoWeb.client.ClientServerConnectorAsync;
import ie.dkit.semanticPhotoWeb.client.dtos.AlbumDTO;
import ie.dkit.semanticPhotoWeb.client.dtos.ImageDTO;
import ie.dkit.semanticPhotoWeb.client.panels.CenterPanel;
import ie.dkit.semanticPhotoWeb.client.panels.photo.UploadPhotoWindow;
import ie.dkit.semanticPhotoWeb.client.panels.tag.TagPanel;
import ie.dkit.semanticPhotoWeb.client.tools.AsyncCallbackAdapter;
import ie.dkit.semanticPhotoWeb.client.tools.ImageHyperlink;
import ie.dkit.semanticPhotoWeb.client.tools.Refreshable;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.MessageBox.ConfirmCallback;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.layout.HorizontalLayout;

/**
 * @author Sebastian Ruehl
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
public class AlbumPanel extends Panel implements Refreshable {

	private class ImageIconListener implements ClickListener {

		private final ImageDTO imageDTO;

		public ImageIconListener(final ImageDTO imageIconDTO) {
			imageDTO = imageIconDTO;
		}

		public void onClick(Widget sender) {
			m_CenterPanel.addPhotoPanel(imageDTO);
		}

	}

	private class ImageIcon extends Panel {
		private final ImageHyperlink link;

		public ImageIcon(final ImageDTO dto) {
			super();
			link = new ImageHyperlink(new Image(dto.thumbnails[1]));
			link.addClickListener(new ImageIconListener(dto));
			setTitle(dto.imageTitle);
			add(link);
		}
	}

	private class deleteAlbumCallbackAdapter extends AsyncCallbackAdapter {
		public void onSuccess(Object result) {
			MessageBox.alert("Album deleted");
			AlbumPanel.this.destroy();
			m_CenterPanel.remove(AlbumPanel.this);
			m_CenterPanel.getUserOverviewPanel().refresh();
		}
	}

	private final ClientServerConnectorAsync m_ClientServerConnectorAsync;

	private final CenterPanel m_CenterPanel;

	private final AlbumDTO m_currentAlbumDTO;

	private Button m_UploadButton;

	private Button m_DeleteButton;

	public AlbumPanel(final AlbumDTO albumDTO,
			final ClientServerConnectorAsync clientServerConnectorAsync,
			final CenterPanel centerPanel) {
		super();
		m_ClientServerConnectorAsync = clientServerConnectorAsync;
		m_CenterPanel = centerPanel;
		m_currentAlbumDTO = albumDTO;
		setClosable(true);
		setAutoScroll(true);
		setTitle(albumDTO.albumName);
		loadImages();
	}

	private void loadImages() {
		clear();
		add(new TagPanel(m_ClientServerConnectorAsync, this));
		Panel horizontalPanel = new Panel();
		horizontalPanel.setLayout(new HorizontalLayout(2));
		horizontalPanel.add(new Button("Open Album Overview",
				new ButtonListenerAdapter() {
					public void onClick(Button button, EventObject e) {
						m_CenterPanel
								.addUserOverviewPanel(m_currentAlbumDTO.username);
					}
				}));
		m_UploadButton = new Button("Upload Photo",
				new ButtonListenerAdapter() {
					public void onClick(Button button, EventObject e) {
						new UploadPhotoWindow(m_UploadButton.getId(),
								m_currentAlbumDTO.username,
								m_currentAlbumDTO.albumName);
					}
				});
		m_UploadButton.setDisabled(true);
		horizontalPanel.add(m_UploadButton);
		m_DeleteButton = new Button("Delete this Album",
				new ButtonListenerAdapter() {
					public void onClick(Button button, EventObject e) {
						MessageBox.confirm("Delete",
								"Are you sure you want to delete this album",
								new ConfirmCallback() {
									public void execute(String btnID) {
										if (btnID.equals("yes")) {
											m_ClientServerConnectorAsync
													.deleteAlbum(
															m_currentAlbumDTO,
															new deleteAlbumCallbackAdapter());
										}
									}
								});
					}
				});
		m_DeleteButton.setDisabled(true);
		horizontalPanel.add(m_DeleteButton);
		add(horizontalPanel);
		m_ClientServerConnectorAsync.getUserName(new AsyncCallback() {
			public void onSuccess(Object result) {
				if (result.equals(m_currentAlbumDTO.username)) {
					m_UploadButton.setDisabled(false);
					m_DeleteButton.setDisabled(false);
				}
			}

			public void onFailure(Throwable caught) {
				// Do nothing
			}
		});
		MessageBox.wait("Loading Pictures...");
		m_ClientServerConnectorAsync.getImages(m_currentAlbumDTO,
				new AsyncCallbackAdapter() {
			
					public void onSuccess(Object arg0) {
						List list = (List) arg0;
						HorizontalPanel horizontalPanel = new HorizontalPanel();
						horizontalPanel.setSpacing(4);
						add(horizontalPanel);
						for (int i = 0; i < list.size(); i++) {
							if (i % 4 == 0) {
								(horizontalPanel = new HorizontalPanel()).setSpacing(4);
								add(horizontalPanel);
							}
							horizontalPanel.add(new ImageIcon((ImageDTO) list.get(i)));
						}
						MessageBox.hide();
						doLayout();
					}

				});
	}

	/**
	 * Return the current displaying album dto
	 * 
	 * @return
	 */
	public AlbumDTO getCurrentAlbumDTO() {
		return m_currentAlbumDTO;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ie.dkit.semanticPhotoWeb.client.tools.Refreshable#refresh()
	 */
	public void refresh() {
		loadImages();
	}
}
